package main

import (
	"net/http"

	"lp/db"

	"lp/api"

	"github.com/gorilla/mux"
	log "github.com/jeanphorn/log4go"
)

//创建路由
var router = mux.NewRouter()

func main() {

	//加载日志配置文件
	log.LoadConfiguration("./logs/log_settings.json")

	log.LOGGER("SERVER").Info("初始化数据库")
	db.InitDB()

	log.LOGGER("SERVER").Info("绑定注册接口")
	router.HandleFunc(api.SIGNUPPORT, api.SignUpHandler)

	log.LOGGER("SERVER").Info("绑定登陆接口")
	router.HandleFunc(api.SIGNINPORT, api.SignInHandler)

	log.LOGGER("SERVER").Info("绑定文件上传接口")
	router.HandleFunc(api.UPLOADFILE, api.UploadHeadPicHandler)

	//头像图片所在文件夹
	const hps = "/images/headpic/"

	//通过正则匹配解析头像链接
	router.Handle(hps+`{.+\..+}`, http.StripPrefix(hps, http.FileServer(http.Dir("."+hps))))
	router.PathPrefix(hps + `{.+\..+}`).Handler(http.StripPrefix(hps, http.FileServer(http.Dir("."+hps))))

	//绑定服务器端口
	http.Handle("/", router)

	//启动服务监听
	log.LOGGER("SERVER").Info("监听端口" + api.SERVERPORT)
	lisErr := http.ListenAndServe(api.SERVERPORT, nil)
	if lisErr != nil {
		log.LOGGER("SERVER").Error("启动服务器监听失败:%v", lisErr)
	}

}
